<?php
	require_once('includes/bootstrap.php');
	
	if (!empty($_POST))
	{
		$price = 0;
		
		foreach ($_SESSION['commande'] as $item)
			$price += $item['quantity'] * $item['price'];
		
		mysql_query(' INSERT INTO orders(client_id, price, date)
									VALUES (' . mysql_real_escape_string($_SESSION['client']['id']) . ', 
													' . mysql_real_escape_string($price) . ', 
													NOW());');
		
		$orderId = mysql_insert_id();
		
		foreach ($_SESSION['commande'] as $item)
		{
			mysql_query(' INSERT INTO order_items(order_id, item_id, quantity)
										VALUES (' . mysql_real_escape_string($orderId) . ', 
														' . mysql_real_escape_string($item['id']) . ', 
														' . mysql_real_escape_string($item['quantity']) . ');');
		}
		
		$_SESSION['notification'] = 'La commande a été validée.';
		
		header('location: ' . APPLICATION_URL);
		exit;
	}
?>

<html>
	<head>
		<?php include_once('includes/head.php'); ?>
	</head>

	<body>

		<?php include_once('includes/header.php'); ?>

		<h2>Confirmation de la commande</h2>
		
		<?php
			if (empty($_SESSION['commande']))
			{
				echo '<br /><br /><p style="text-align: center; font-style: italic">Votre panier est vide</p>';
			}
			else
			{
		?>
		
		<form method="POST" id="basket">
			
			<input type="hidden" name="toto" value="1" />
			
			<table>
				<tr>
					<td colspan="3" style="text-align: center; font-size: 20px;">Panier</td>
				</tr>
				<tr>
					<th>Article</th>
					<th style="width: 100px;">Quantité</th>
					<th style="width: 75px;">Prix</th>
				</tr>
				<?php
					$itemsCount = 0;
					$price = 0;
				
					foreach ($_SESSION['commande'] as $item)
					{
						echo '<tr>';
						echo '	<td>' . $item['name'] . '</td>';
						echo '	<td class="center">' . $item['quantity'] . '</td>';
						echo '	<td class="center">' . $item['quantity'] * $item['price'] . ' €</td>';
						echo '</tr>';
						
						$itemsCount += $item['quantity'];
						$price += $item['quantity'] * $item['price'];
					}
				?>
				<tr style="background: lightgrey">
					<td>Total</td>
					<td class="center"><?= $itemsCount ?></td>
					<td class="center"><?= $price ?> €</td>
				</tr>
				<tr>
					<td colspan="3">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="text-align: center; font-size: 20px;">Adresse de livraison</td>
				</tr>
				<tr>
					<td colspan="3" style="text-align: center; font-size: 14px;">
						<?php
							echo $_SESSION['client']['firstname'] . ' ' . $_SESSION['client']['name'] . '<br />';
							echo $_SESSION['client']['address'] . '<br /><br />';
							echo $_SESSION['client']['phone_number'];
						?>
					</td>
				</tr>
				<tr>
					<td colspan="3">&nbsp;</td>
				</tr>
				<tr>
					<td colspan="3" style="text-align: right">
						<button type="button" name="go_back" onclick="goBack()">Continuer le shopping</button>
						<button type="button" name="valider" onclick="confirmCommande()">Valider la commande</button>
					</td>
				</tr>
			</table>
			
		</form>
		
		<script type="text/javascript">
			$("input:text, input:password, textarea").addClass("ui-corner-all");
			
			$('form table').addClass('ui-widget ui-widget-content ui-corner-all');

			$("button").button();
			
			function confirmCommande()
			{
				if (confirm('Etes-vous sûr de valider définitivement votre commande ?'))
					$('#basket').submit();
			}
			
			function goBack()
			{
				location.href = '<?= addslashes(APPLICATION_URL) ?>';
			}
		</script>
		
		<?php } ?>

	</body>

</html>
